/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.entities;

import edu.soc.cs5226.gui.entities.util.Constants;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author huijia
 */
public class SortMemoryProcessList {
    public dayObject processTreeNode(Timestamp now, List<xObject> xList, HealthStatus hs)
    {
        long total24Number = 0;
        long total24SortsMemory = 0;
        long total24SortsDisk = 0;
        long total24SortingToDisk = 0;
        
        Iterator<xObject> xIter = xList.iterator();
        while (xIter.hasNext())
        {
            long totalNumber = 0;
            long totalSortsMemory = 0;
            long totalSortsDisk = 0;
            long totalSortingToDisk  = 0;
            xObject currX = xIter.next();
            Iterator<yObject> yIter = currX.getyObjects().iterator();
            while (yIter.hasNext())
            {
                long subtotalNumber = 0;
                long subtotalSortsMemory = 0;
                long subtotalSortsDisk = 0;
                long subtotalSortingToDisk  = 0;
                yObject currY = yIter.next();
                Iterator<DBParameter> smIter = currY.getObj().iterator();
                while (smIter.hasNext())
                {
                    SortMemory smObj = (SortMemory)smIter.next();
                    total24SortsMemory = total24SortsMemory + smObj.getSortsMemory();
                    total24SortsDisk = total24SortsDisk + smObj.getSortsDisk();
                    total24SortingToDisk = total24SortingToDisk + smObj.getSortingToDisk();
                    total24Number++;
                    
                    totalSortsMemory = totalSortsMemory + smObj.getSortsMemory();
                    totalSortsDisk = totalSortsDisk + smObj.getSortsDisk();
                    totalSortingToDisk = totalSortingToDisk + smObj.getSortingToDisk();
                    totalNumber++;
                    
                    subtotalSortsMemory = subtotalSortsMemory + smObj.getSortsMemory();
                    subtotalSortsDisk = subtotalSortsDisk + smObj.getSortsDisk();
                    subtotalSortingToDisk = subtotalSortingToDisk + smObj.getSortingToDisk();
                    subtotalNumber++;
                }
                SortMemory treenodeY = (SortMemory)currY.getyObj();
                treenodeY.setSortsMemory(calcAverage(subtotalSortsMemory,subtotalNumber));
                treenodeY.setSortsDisk(calcAverage(subtotalSortsDisk,subtotalNumber));
                treenodeY.setSortingToDisk(calcAverage(subtotalSortingToDisk,subtotalNumber));
                treenodeY.setS_healthStatus(processSortMemory(treenodeY, hs));
            }
            SortMemory treenodeX = (SortMemory)currX.getxObj();
            treenodeX.setSortsMemory(calcAverage(totalSortsMemory,totalNumber));
            treenodeX.setSortsDisk(calcAverage(totalSortsDisk,totalNumber));
            treenodeX.setSortingToDisk(calcAverage(totalSortingToDisk,totalNumber));
            treenodeX.setS_healthStatus(processSortMemory(treenodeX, hs));
        }
        
        SortMemory treenodeRoot = (SortMemory)DBParamFactory.getDBParameter(Constants.SORTMEMORY);
        treenodeRoot.setSortsMemory(calcAverage(total24SortsMemory,total24Number));
        treenodeRoot.setSortsDisk(calcAverage(total24SortsDisk,total24Number));
        treenodeRoot.setSortingToDisk(calcAverage(total24SortingToDisk,total24Number));
        treenodeRoot.setS_healthStatus(processSortMemory(treenodeRoot, hs));
        treenodeRoot.setdateTime(new Timestamp(now.getTime()-(24*60*60*1000)));
        
        dayObject dObject = new dayObject();
        dObject.setRootObj(treenodeRoot);
        dObject.setxObjects(xList);
        
        return dObject;
    }
    
    //--------------------------------------------------------------------------
    
    private long calcAverage(long value, long number)
    {
        if (value>0)
            return (value/number);
        else
            return 0;
    }
    
    //--------------------------------------------------------------------------
    // processSortMemoryArea obtains sharedpoolsize info from the database
    // and calculcates the status indicator
    // if the sortingToDisk < yellow indicator
    //    status is green.
    // if the sortingToDisk >= yellow & sortingToDisk < red indicator
    //    status is yellow
    // if the sortingToDisk >= red indicator
    //    status is red
    //--------------------------------------------------------------------------
    private String processSortMemory(SortMemory sm, HealthStatus hs)
    {
        String status = "G";
        if ((sm.getSortingToDisk()>=hs.getyellowThres()) && 
                (sm.getSortingToDisk()<hs.getredThres()))
        {
            status = "Y";
        }
        else if (sm.getSortingToDisk()>=hs.getredThres())
        {
            status = "R";
        }
        return status;
    } // processSortMemory
}
